import { Component } from '@angular/core';
import { NavController, NavParams, ToastController, LoadingController, Events } from 'ionic-angular';
import {Camera, CameraOptions} from "@ionic-native/camera";
import { BaseUI } from '../../common/baseui';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
// import { File } from '@ionic-native/file';
import { TabsPage } from '../tabs/tabs';
import { ApiProvider } from '../../providers/api/api';
/**
 * Generated class for the AuthPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

// @IonicPage()
@Component({
  selector: 'page-auth',
  templateUrl: 'auth.html',
})
export class AuthPage extends BaseUI {
  public idNum: String;
  public idFrontImg: String;
  public idBackImg: String;
  path = '';
  public idFrontUuid = '';
  public idBackUuid = '';
  constructor(public navCtrl: NavController,
    public navParams: NavParams,
    public camera: Camera,
    public toastCtrl: ToastController,
    public loadingCtrl: LoadingController,
    private transfer: FileTransfer,
    // private file: File,
    public api: ApiProvider,
    public events: Events
    ) {
    super();
  }
  openCamera(picType: string){
    const options: CameraOptions = {
      quality: 50,                                                   //相片质量 0 -100
      allowEdit: true,
      targetWidth: 200,
      targetHeight: 200,
      destinationType: this.camera.DestinationType.DATA_URL,        //DATA_URL 是 base64   FILE_URI 是文件路径
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      saveToPhotoAlbum: true,                                       //是否保存到相册
      sourceType: this.camera.PictureSourceType.CAMERA,         //是打开相机拍照还是打开相册选择  PHOTOLIBRARY : 相册选择, CAMERA : 拍照,
    }

    this.camera.getPicture(options).then((imageData) => {
      console.log("got file: " + imageData);

      // If it's base64:
      let base64Image = 'data:image/jpeg;base64,' + imageData;
      console.log('base64Image=' + base64Image);
      this.path = base64Image;

      //If it's file URI
      this.upload(picType);

    }, (err) => {
      // Handle error
      super.showToast(this.toastCtrl, '拍摄失败，请重试！');
    });
  }
  upload(picType: string) {
    let apiPath = ''
    if(picType === 'front'){
      apiPath = this.api.uploadFileToOssFront;
    }else{
      apiPath = this.api.uploadFileToOssBack;
    }
    const fileTransfer: FileTransferObject = this.transfer.create();
    let options: FileUploadOptions = {
      fileKey: 'file',
      fileName: 'avatar.jpg',
      headers: {}
      // params: {
      //   picFile: this.path
      // }
    }
    let loading = super.showLoading(this.loadingCtrl, "上传中")
    fileTransfer.upload(this.path, apiPath, options)
      .then((res) => {
        loading.dismiss()
        let result =  eval('(' + res['response'] + ')');
        let uuid = '';
        if (picType === 'front') {
          if(result['code'] === 200){
            uuid = `https://shudai17919.oss-cn-shenzhen.aliyuncs.com/${result['result']['picUrl']}`;
            this.idNum = result['result']['idcard'];
            this.idFrontImg = uuid;
            this.idFrontUuid = uuid;
          }else{
            super.showToast(this.toastCtrl, result['msg'])
            return
          }
        } else {
          if(result['code'] === 500){
            super.showToast(this.toastCtrl, result['msg'])
            return
          }
          uuid = `https://shudai17919.oss-cn-shenzhen.aliyuncs.com/${result['result']}`;
          this.idBackImg = uuid;
          this.idBackUuid = uuid;
        }
        super.showToast(this.toastCtrl, '图片上传成功');
      }, (err) => {
        loading.dismiss()
        super.showToast(this.toastCtrl, '系统故障，请稍后再试');
        if(err == 401 || err == 500){
          window.localStorage.removeItem('phoneNum')
          this.events.publish('toLogin');
        }
      });
  }
  realnameAuth(){
    console.log('idFrontUuid:' + this.idFrontUuid + '  idBackUuid:' + this.idBackUuid);
    if (this.idFrontUuid.length == 0) {
      super.showToast(this.toastCtrl, '请上传身份证人像面照片');
      return;
    }
    if (this.idBackUuid.length == 0) {
      super.showToast(this.toastCtrl, '请上传身份证国徽面照片');
      return;
    }
    let params = {
      idNum: this.idNum,
      idcardFaceUrl: this.idFrontUuid,
      idcardBackUrl: this.idBackUuid
    };
    let loading = super.showLoading(this.loadingCtrl, "提交中")
    this.api.saveRealInfo(params).subscribe(f => {
      console.log('提交成功');
      loading.dismiss();
      window.localStorage.removeItem('realNameNeed');
      window.localStorage.removeItem('realNameVerify');
      this.navCtrl.setRoot(TabsPage);
    }, err => {
      loading.dismiss();
      super.showToast(this.toastCtrl, '提交失败, 请重试');
      if(err == 401 || err == 500){
        window.localStorage.removeItem('phoneNum')
        this.events.publish('toLogin');
      }
    })
  }
   //文件下载
  //  download() {
  //   const fileTransfer: FileTransferObject = this.transfer.create();
  //   const url ="http://www.example.com/file.pdf";
  //   fileTransfer.download(url, this.file.dataDirectory + 'file.jpg').then((entry) => {
  //     this.image1=entry.toURL();
  //   }, (error) => {
  //     alert("download失败")
  //   });
  // }
  // 实名认证页面埋点
  activeEvent(){
    let params = {
      'event': 'APP_RNC',
      'phoneNum': window.localStorage.getItem('phoneNum')
    }
    this.api.addEvent(params).subscribe(g => {
      console.log('实名认证埋点触发', g);
    }, err => {
      if(err == 401 || err == 500){
        window.localStorage.removeItem('phoneNum')
        this.events.publish('toLogin');
      }
    })
  }
  ionViewDidLoad() {
    this.activeEvent();
    this.idFrontImg = 'assets/imgs/id_front.png';
    this.idBackImg = 'assets/imgs/id_back.png';
  }

}
